package com.hongtu.brank1.service.impl;

import com.hongtu.brank1.fegin.Bank2Client;
import com.hongtu.brank1.mapper.AccountInfoMapper;
import com.hongtu.brank1.service.AccountInfoService;
import io.seata.core.context.RootContext;
import io.seata.spring.annotation.GlobalTransactional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class AccountInfoServiceImpl implements AccountInfoService {
    private Logger logger = LoggerFactory.getLogger(AccountInfoServiceImpl.class);


    @Autowired
    AccountInfoMapper accountInfoDao;
    @Autowired
    Bank2Client bank2Client;

    @GlobalTransactional
    @Transactional
    @Override
    public void updateAccountBalance(String accountNo, Double amount) {
        logger.info("******** Bank1 Service Begin ... xid: {}", RootContext.getXID());
        accountInfoDao.updateAccountBalance(accountNo, amount * -1);
        String remoteRst = bank2Client.transfer(amount);

        //远程调用失败
        if(remoteRst.equals("fallback")){
            throw new RuntimeException("bank1 下游服务异常");
        }

    }
}
